package com.aios.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aios.dao.DayScheduleMapper;
import com.aios.dao.TimerMapper;
import com.aios.entity.DaySchedule;
import com.aios.entity.Timer;
import com.aios.entity.mapper.TimerSetting;
import com.aios.entity.mapper.TimerSwitch;
import com.aios.entity.unit.StatusEnum;
import com.aios.service.TimerService;
import com.aios.util.GuidGenerator;
import com.aios.util.ListCompare;
import com.aios.util.Pager;

@Service("timerService")
public class TimerServiceImpl  extends AbstractListService implements TimerService{

	@Autowired
	private TimerMapper timerMapper;
	
	@Autowired
	private DayScheduleMapper dayScheduleMapper;
	
	private String userid;
	
	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	@Override
	public Timer findById(String id){
		return timerMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public List<Timer> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Timer> findAllNotDisabled() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Timer> findAllDisabled() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int save(Timer entity) {
		return timerMapper.insert(entity);
	}

	@Override
	public int update(Timer entity) {
		return timerMapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public int delete(Timer id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int delete(String id) {
		return timerMapper.deleteByPrimaryKey(id);
	}

	
	@Override
	public Pager viewPaginatedActiveList(Integer start, Integer size) {
		prepare(start, size);
		try {
			pager.setTotal(timerMapper.countAllByStatus(StatusEnum.ACTIVE.getStatusValue()));
			pager.setList(timerMapper.findListPaginated(StatusEnum.ACTIVE.getStatusValue(), start, size));
		}catch(Exception e) {
			//logger
			e.printStackTrace();
		}
		
		return pager;
	}



	@Override
	public TimerSetting findByIdWithSchedule(String id) {
		
		return timerMapper.findByIdWithSchedule(id);
	}

	@Override
	public int save(Timer entity, String userId,Byte status) {
		int returnVal = 0;
		entity.setStatus(status);
		entity.setId(GuidGenerator.timerKey());
		returnVal = save(entity);
		return returnVal;
	}

	@Override
	public int update(Timer entity, String userId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<TimerSwitch> findTimerByUserId(String id) {
		return timerMapper.findTimerByUserId(id);
	}

	@Override
	public int findDaySchedule(TimerSetting timerSetting,
			List<Integer> dayList,String uid) {

		userid=uid;
		int returnValue=0;
		if(dayList == null){
			dayList= new ArrayList<Integer>();
		}
		List<Integer> existingList = dayScheduleMapper.findByTimerId(timerSetting.getTimer().getId());
		List<Integer> differenceList = ListCompare.getDifference(dayList,existingList);
		
		if (!(differenceList.isEmpty())) {
			returnValue = this.removeUncheckedDay(timerSetting.getTimer().getId(),differenceList);
		}
		differenceList = ListCompare.getDifference(existingList, dayList);

		if (!(differenceList.isEmpty())) {
			returnValue = this.addNewDays(timerSetting.getTimer().getId(), differenceList);
		}
		return returnValue;
	}

	public int addNewDays(String id, List<Integer> differenceList) {
		int returnValue=0;
		for(int i=0;i<differenceList.size();i++){
			DaySchedule daySchedule= new DaySchedule();
			saveAddSettings(daySchedule,userid);
			daySchedule.setId(GuidGenerator.dayScheduleKey());
			daySchedule.setTimerId(id);
			daySchedule.setDayOf(differenceList.get(i).byteValue());
			returnValue=this.dayScheduleMapper.insert(daySchedule);
		}

		return returnValue;
	}

	private int removeUncheckedDay(String id, List<Integer> differenceList) {
		
		int returnValue=0;
		
		for(int i=0; i < differenceList.size(); i++) {
			returnValue=this.dayScheduleMapper.deleteByTimerIdAndDayOf(id, differenceList.get(i));
		}
		
		return returnValue;
	}

	@Override
	public int save(Timer entity, String userId) {
		// TODO Auto-generated method stub
		return 0;
	}
	
}
